package service.impl.v2;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.v2.AwardVOMapper;
import dao.v2.ConfigVOMapper;
import dao.v2.MemberVOMapper;
import dao.v2.MessageVOMapper;
import dao.v2.SplitVOMapper;
import model.AwardVO;
import model.LogEarnVO;
import model.LogTicketVO;
import model.MemberTicketVO;
import model.MemberVO;
import service.v2.PushService;
import service.v2.SplitService;
import util.NumberUtil;
import util.PropertieUtil;
import util.StringUtil;

@Service
public class SplitServiceImpl implements SplitService {
	@Autowired
	private AwardVOMapper awardDao;
	@Autowired
	private SplitVOMapper splitDao;
	@Autowired
	private ConfigVOMapper configDao;
	@Autowired
	private MemberVOMapper memberDao;
	@Autowired
	private MessageVOMapper msgDao;
	@Autowired
	private PushService pushService;

	// 只产生入会奖
	public void splitJoinAward(String member_id, String level, int type, String num, String buy_rate, String order_id,
			String join_cash, String pay_type) throws Exception {
		String country = splitDao.getCountryByOrder(order_id);
		// 奖项
		AwardVO joinAward = null;
		AwardVO dynamicAward = null;
		AwardVO staticAward = null;
		AwardVO masterAward = null;
		AwardVO partnerAward = null;
		AwardVO areaAward = null;
		AwardVO cityAward = null;
		// 等级列表
		String common_level = PropertieUtil.loadPropertie("common_level");
		String vip_level = PropertieUtil.loadPropertie("vip_level");
		String primary_level = PropertieUtil.loadPropertie("primary_level");
		String middle_level = PropertieUtil.loadPropertie("middle_level");
		String teacher_level = PropertieUtil.loadPropertie("teacher_level");
		String expert_level = PropertieUtil.loadPropertie("expert_level");
		String master_level = PropertieUtil.loadPropertie("master_level");
		String partner_level = PropertieUtil.loadPropertie("partner_level");
		String area_level = PropertieUtil.loadPropertie("area_level");
		String city_level = PropertieUtil.loadPropertie("city_level");
		// 获取奖项信息
		List<AwardVO> awardList = awardDao.getAwardAll();
		for (AwardVO awardVO : awardList) {
			if ("join_award".equals(awardVO.getCal())) {
				joinAward = awardVO;
			} else if ("dynamic_award".equals(awardVO.getCal())) {
				dynamicAward = awardVO;
			} else if ("static_award".equals(awardVO.getCal())) {
				staticAward = awardVO;
			} else if ("super_award".equals(awardVO.getCal())) {
				masterAward = awardVO;
			} else if ("partner_award".equals(awardVO.getCal())) {
				partnerAward = awardVO;
			} else if ("area_award".equals(awardVO.getCal())) {
				areaAward = awardVO;
			} else if ("city_award".equals(awardVO.getCal())) {
				cityAward = awardVO;
			}
		}
		// 查出所有上级会员
		String ids_str = splitDao.getParentIds(member_id);
		String[] ids = ids_str.split(",");
		// 分润
		if (ids.length > 1) {

			// 入会奖 type2购物 type3优惠套餐
			if ((type == 2 || type == 3) && Double.parseDouble(join_cash) > 0) {
				// 判断购买人级别是否是普通会员
				// 查找最近的,状态正常的父级
				String id = null;
				for (int i = ids.length - 2; i >= 0; i--) {
					// 条件：1.id符合 2.等级符合 3.状态正常
					id = splitDao.getJoinAwardFitMember(ids[i], common_level);
					if (id != null) {
						break;
					}
				}
				if(id != null){
					//判断国家
					// 现金入账 入会奖
					splitDao.insertCashInAwardLogJoin(id, joinAward.getId(), join_cash, member_id, order_id, type, country);
					// 购买套餐 如果是普通会员升级为高级会员
					if (type == 3 || type==1) {
//						splitDao.joinAwardLevelUpNow(member_id, vip_level, common_level); 需求更变 入会奖不升级
						splitDao.addJoinAwardCash(id, join_cash, country);
					}
				}
			}
		}
	}

	// 分润
	// type 1充值 2原价购买商品 3优惠套餐
	// num 提货券,充值的或购入的 member_id 会员ID level会员等级 buy_rate兑换券购买折扣(原价1)
	// order_id订单id没有传null join_cash入会奖金额(没有传0) pay_type 支付方式(兑换券 1.余额2.支付宝3.微信4.payssion5.paypal)
	public void split(String member_id, String level, int type, String num, String buy_rate, String order_id,
			String join_cash, String pay_type, String country) throws Exception {
		if(country == null){
			country = splitDao.getCountryByOrder(order_id);
		}
		
		String conn_no = "";
		if (num == null || "0".equals(num) || "".equals(num) || !StringUtil.isNumeric(num)) {
			return;
		}
		if (join_cash == null) {
			join_cash = "0";
		}
		if(type==1){
			conn_no = order_id;
			order_id = null;
		}
		// 奖项
		AwardVO joinAward = null;
		AwardVO dynamicAward = null;
		AwardVO staticAward = null;
		AwardVO masterAward = null;
		AwardVO partnerAward = null;
		AwardVO companyAward = null;
		AwardVO pushAward = null;
		AwardVO areaAward = null;
		AwardVO cityAward = null;
		// 等级列表
		String common_level = PropertieUtil.loadPropertie("common_level");
		String vip_level = PropertieUtil.loadPropertie("vip_level");
		String primary_level = PropertieUtil.loadPropertie("primary_level");
		String middle_level = PropertieUtil.loadPropertie("middle_level");
		String teacher_level = PropertieUtil.loadPropertie("teacher_level");
		String expert_level = PropertieUtil.loadPropertie("expert_level");
		String master_level = PropertieUtil.loadPropertie("master_level");
		String partner_level = PropertieUtil.loadPropertie("partner_level");
		String area_level = PropertieUtil.loadPropertie("area_level");
		String city_level = PropertieUtil.loadPropertie("city_level");
		// 获取奖项信息
		List<AwardVO> awardList = awardDao.getAwardAll();
		for (AwardVO awardVO : awardList) {
			if ("join_award".equals(awardVO.getCal())) {
				joinAward = awardVO;
			} else if ("dynamic_award".equals(awardVO.getCal())) {
				dynamicAward = awardVO;
			} else if ("static_award".equals(awardVO.getCal())) {
				staticAward = awardVO;
			} else if ("super_award".equals(awardVO.getCal())) {
				masterAward = awardVO;
			} else if ("partner_award".equals(awardVO.getCal())) {
				partnerAward = awardVO;
			} else if ("company_award".equals(awardVO.getCal())) {
				companyAward = awardVO;
			} else if ("push_award".equals(awardVO.getCal())) {
				pushAward = awardVO;
			} else if ("area_award".equals(awardVO.getCal())) {
				areaAward = awardVO;
			} else if ("city_award".equals(awardVO.getCal())) {
				cityAward = awardVO;
			}
		}
		// 待扣提货券
		int leftNum = Integer.parseInt(num);
		// 查出所有上级会员
		String ids_str = splitDao.getParentIds(member_id);
		String[] ids = ids_str.split(",");
		// 分润
		if (ids.length > 1) {
			//判断国家

			// 入会奖 type2购物 type3优惠套餐
			if ((type == 2 || type == 3) && Double.parseDouble(join_cash) > 0) {
				// 查找最近的,状态正常的父级
				String id = null;
				for (int i = ids.length - 2; i >= 0; i--) {
					// 条件：1.id符合 2.等级符合 3.状态正常
					id = splitDao.getJoinAwardFitMember(ids[i], common_level);
					if (id != null) {
						break;
					}
				}
				if(id != null){
					// 现金入账 入会奖
					splitDao.insertCashInAwardLogJoin(id, joinAward.getId(), join_cash, member_id, order_id, type, country);
					// 购买套餐 如果是普通会员升级为高级会员
					if (type == 3 || type==1) {
//						splitDao.joinAwardLevelUpNow(member_id, vip_level, common_level);需求更变 入会奖不升级
						splitDao.addJoinAwardCash(id, join_cash, country);
					}
				}
			}

			// 最低折扣
			double minRate = splitDao.getMinRate();

			// 大师奖 购买人是大师
			if (master_level.equals(level) && buy_rate.equals(NumberUtil.formatDouble(minRate, 2))) {
				int masterNum = (int) Double.parseDouble(masterAward.getRate1());// 大师找几级
				int rateInd = 2;
				for (int i = ids.length - 2; i >= 0; i--) {
					// 条件：1.id符合 2.等级符合 3.状态正常
					String parentId = splitDao.getMasterAwardFitMember(ids[i], master_level, partner_level);
					// 生成大师奖日志 大师购买 获奖人是大师 向上找masterNum
					if (masterNum != 0 && parentId != null) {
						String rate = null;
						if (rateInd == 2) {
							rate = masterAward.getRate2();
						} else if (rateInd == 3) {
							rate = masterAward.getRate3();
						} else if (rateInd == 4) {
							rate = masterAward.getRate4();
						} else if (rateInd == 5) {
							rate = masterAward.getRate5();
						}
						// 大师获得现金收益日志
						splitDao.insertCashInLogMaster(type, ids[i], num, masterAward, order_id, member_id, rate ,conn_no, country);
						// 大师获得购物专用金日志
						splitDao.insertBuyCashInLogMaster(type, ids[i], num, masterAward, order_id, member_id, rate, conn_no, country);
						if (type == 1 || type == 3) {
							splitDao.updateMasterSettle(type, ids[i], num, masterAward, order_id, member_id, rate, country);
						}
						masterNum--;
						rateInd++;
					}
					if (masterNum == 0) {
						break;
					}
				}
			}
			
			// 合伙人奖 购买人是合伙人
			if (partner_level.equals(level) && buy_rate.equals(NumberUtil.formatDouble(minRate, 2))) {
				int companyNum = (int) Double.parseDouble(companyAward.getRate1());// 大师找几级
				int rateInd = 2;
				for (int i = ids.length - 2; i >= 0; i--) {
					// 条件：1.id符合 2.等级符合 3.状态正常
					String parentId = splitDao.getCompanyAwardFitMember(ids[i], partner_level);
					// 生成大师奖日志 大师购买 获奖人是大师 向上找masterNum
					if (companyNum != 0 && parentId != null) {
						String rate = null;
						if (rateInd == 2) {
							rate = companyAward.getRate2();
						} else if (rateInd == 3) {
							rate = companyAward.getRate3();
						} else if (rateInd == 4) {
							rate = companyAward.getRate4();
						} else if (rateInd == 5) {
							rate = companyAward.getRate5();
						}
						// 合伙人获得现金收益日志
						splitDao.insertCashInLogCompany(type, ids[i], num, companyAward, order_id, member_id, rate, conn_no, country);
						// 大师获得购物专用金日志
						splitDao.insertBuyCashInLogCompany(type, ids[i], num, companyAward, order_id, member_id, rate, conn_no, country);
						if (type == 1 || type == 3) {
							splitDao.updateCompanySettle(type, ids[i], num, companyAward, order_id, member_id, rate, country);
						}
						companyNum--;
						rateInd++;
					}
					if (companyNum == 0) {
						break;
					}
				}
			}
			
			// 直推奖 购买人是合伙人
			if (partner_level.equals(level)) {
				String parentId = splitDao.getPushAwardFitMember(ids[ids.length - 2],vip_level);
				if(parentId != null){
					// 生成直推奖现金收益日志
					splitDao.insertCashInLogPush(type, parentId, num, pushAward, order_id, member_id, conn_no,country);
					//消息+1
					// type1充值成功直接结算
					if (type == 1 || type == 3) {
						// 增加余额 增加购物金
						splitDao.updatePushSettle(parentId, num, pushAward, country);
					}
				}
			}

			// 动销奖 补贴奖
			for (int i = ids.length - 2; i >= 0; i--) {
				// 获取会员的等级 并验证状态
				String parentLevel = splitDao.getLevelById(ids[i]);
				// 为空则状态不正常 跳过
				if (parentLevel == null) {
					continue;
				}
				
				// 1验证卖家不是合伙人且有折扣差  2卖家是合伙人 买家等级低于合伙人
//				int fitNum = splitDao.checkParentLevelAwardFit(parentLevel, level);
//				if ((partner_level.equals(parentLevel) && fitNum > 0) || !partner_level.equals(parentLevel) || type==2 || type==3) {
					// 上步执行通过 扣券开始
					// 锁定用户券
					int result = 0;
					do {
						result = splitDao.lockMemberTicket(ids[i]);
						if (result == 0) {
							Thread.sleep(100);
						}
					} while (result == 0);
					// 商票
					List<MemberTicketVO> ticketList = null;
					
					// 1.合伙人 销售折扣为最低折扣  0利润动销奖 产生合伙人奖
					if (partner_level.equals(parentLevel) && buy_rate.equals(NumberUtil.formatDouble(minRate, 2)) && master_level.equals(level)) {
						ticketList = splitDao.getHasTicketMemberParter(ids[i], buy_rate);
						// 2.分销商 销售折扣< 产生动销奖
					} else {
						ticketList = splitDao.getHasTicketMemberAgency(ids[i], buy_rate);
					}
					if (ticketList.size() != 0) {
						for (MemberTicketVO memberTicketVO : ticketList) {
							// 可卖提货券大于购买提货券
							if (Integer.parseInt(memberTicketVO.getNum()) > leftNum) {
								// 拆分提货券
								// 修改原提货券为剩余提货券
								int re = splitDao.updateSplitTicket(memberTicketVO.getId(), leftNum);
								if (re > 0) {
									// 添加卖掉的提货券并关联订单 关联原提货券单号
									MemberTicketVO ticketVO = new MemberTicketVO();
									splitDao.insertSellTicket(ticketVO, memberTicketVO.getId(), leftNum, order_id,
											memberTicketVO.getMember_id());
									// 奖项分配
									genDynamicAndParterAward(member_id, type, buy_rate, order_id, dynamicAward, staticAward,
											partnerAward, partner_level, ids, leftNum, i, parentLevel, memberTicketVO,
											ticketVO.getId(),conn_no,level,country);
									// 提货券销售完毕
									leftNum = 0;
								}
								// 可卖提货券等于购买提货券
							} else if (Integer.parseInt(memberTicketVO.getNum()) == leftNum) {
								// 原提货券关联订单
								int re = splitDao.updateSplitTicketOrderEq(memberTicketVO.getId(), order_id, leftNum);
								if (re > 0) {
									// 奖项分配
									genDynamicAndParterAward(member_id, type, buy_rate, order_id, dynamicAward, staticAward,
											partnerAward, partner_level, ids, leftNum, i, parentLevel, memberTicketVO,
											memberTicketVO.getId(),conn_no,level,country);
									// 提货券销售完毕
									leftNum = 0;
								}
								// 可卖提货券小于购买提货券
							} else {
								// 扣除原提货券并关联订单
								int re = splitDao.updateSplitTicketOrderLt(memberTicketVO.getId(), order_id,
										Integer.parseInt(memberTicketVO.getNum()));
								if (re > 0) {
									// 奖项分配
									genDynamicAndParterAward(member_id, type, buy_rate, order_id, dynamicAward, staticAward,
											partnerAward, partner_level, ids, Integer.parseInt(memberTicketVO.getNum()), i,
											parentLevel, memberTicketVO, memberTicketVO.getId(),conn_no,level,country);
									// 提货券销售部分
									leftNum = leftNum - Integer.parseInt(memberTicketVO.getNum());
								}
							}
							// 扣除完毕退出该会员提货券循环
							if (leftNum == 0) {
								break;
							}
						}
					}
					
					// 扣除完毕退出上级会员循环循环
					if (leftNum == 0) {
						// 券锁定解除
						splitDao.unLockMemberTicket(ids[i]);
						break;
					}
					// 券锁定解除
					splitDao.unLockMemberTicket(ids[i]);
//				}
				
			}
			
			//判断是否产生动销奖
			//if(Integer.parseInt(num) > leftNum){
				//常销奖
				if (Double.parseDouble(buy_rate) > minRate) {
					String firstParentId = null;
					for (int i = ids.length - 2; i >= 0; i--) {
						// 条件：1.id符合 2.等级符合 3.状态正常
						firstParentId = splitDao.getStaticAwardFitMember(ids[i], vip_level);
						if (firstParentId != null) {
							// 生成常销奖现金收益日志
							splitDao.insertCashInLogStatic(type, firstParentId, num, staticAward, order_id, member_id, conn_no, country);
							// 生成常销奖购物专用金收益日志
							splitDao.insertBuyCashInLogStatic(type, firstParentId, num, staticAward, order_id, member_id, conn_no, country);
							// type1充值成功直接结算
							if (type == 1 || type == 3) {
								// 增加余额 增加购物金
								splitDao.updateStaticSettle(firstParentId, num, staticAward, country);
							}
							// 结束跳出
							break;
						}
					}
				}
			}

		//}
		
		//买的人需实名认证城市 可扣区代市代
//		MemberVO buyMember = memberDao.getAllInfoById(member_id); 不用了 旧版
//		int address_num = splitDao.getAddressNum(member_id);
//		if(address_num > 0){
			// 兑换券没扣完 区代售出
			if (leftNum > 0) {
				// 代理售出
				// 查询所在区区代
				MemberVO memberVO = splitDao.getAreaAgencyByCity(member_id, area_level);
				if (memberVO != null) {
					//查询购买人级别低于区代
					int fitNum = splitDao.checkParentLevelAwardFit(memberVO.getLevel(), level);
					if(fitNum > 0){
						// 锁定用户券
						int result = 0;
						do {
							result = splitDao.lockMemberTicket(memberVO.getId());
							if (result == 0) {
								Thread.sleep(100);
							}
						} while (result == 0);
						// 商票
						List<MemberTicketVO> ticketList = null;
						double minRate = splitDao.getMinRate();
						// 1.同折扣销售必须为最低折扣
						if (buy_rate.equals(NumberUtil.formatDouble(minRate, 2))) {
							ticketList = splitDao.getHasTicketMemberArea(memberVO.getId(), buy_rate);
							// 2.分销商 销售折扣< 产生动销奖
						} else {
							ticketList = splitDao.getHasTicketMemberAreaLt(memberVO.getId(), buy_rate);
						}
						if (ticketList.size() != 0) {
							for (MemberTicketVO memberTicketVO : ticketList) {
								// 可卖提货券大于购买提货券
								if (Integer.parseInt(memberTicketVO.getNum()) > leftNum) {
									// 拆分提货券
									// 修改原提货券为剩余提货券
									int re = splitDao.updateSplitTicket(memberTicketVO.getId(), leftNum);
									if (re > 0) {
										// 添加卖掉的提货券并关联订单 关联原提货券单号
										MemberTicketVO ticketVO = new MemberTicketVO();
										splitDao.insertSellTicket(ticketVO, memberTicketVO.getId(), leftNum, order_id,
												memberTicketVO.getMember_id());
										// 奖项分配
										genDynamicAndAreaAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, areaAward, area_level, memberVO, leftNum, memberTicketVO,
												ticketVO.getId(),conn_no, country);
										// 提货券销售完毕
										leftNum = 0;
									}
									// 可卖提货券等于购买提货券
								} else if (Integer.parseInt(memberTicketVO.getNum()) == leftNum) {
									// 原提货券关联订单
									int re = splitDao.updateSplitTicketOrderEq(memberTicketVO.getId(), order_id, leftNum);
									if (re > 0) {
										// 奖项分配
										genDynamicAndAreaAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, areaAward, area_level, memberVO, leftNum, memberTicketVO,
												memberTicketVO.getId(),conn_no, country);
										// 提货券销售完毕
										leftNum = 0;
									}
									// 可卖提货券小于购买提货券
								} else {
									// 扣除原提货券并关联订单
									int re = splitDao.updateSplitTicketOrderLt(memberTicketVO.getId(), order_id,
											Integer.parseInt(memberTicketVO.getNum()));
									if (re > 0) {
										// 奖项分配
										genDynamicAndAreaAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, areaAward, area_level, memberVO, Integer.parseInt(memberTicketVO.getNum()), memberTicketVO,
												memberTicketVO.getId(),conn_no, country);
										// 提货券销售部分
										leftNum = leftNum - Integer.parseInt(memberTicketVO.getNum());
									}
								}
								// 扣除完毕退出该会员提货券循环
								if (leftNum == 0) {
									break;
								}
							}
						}
						// 券锁定解除
						splitDao.unLockMemberTicket(memberVO.getId());
					}
					
				}
			}
			
			// 兑换券没扣完 市代售出
			if (leftNum > 0) {
				// 代理售出
				// 查询所在区市代
				MemberVO memberVO = splitDao.getCityAgencyByCity(member_id, city_level);
				if (memberVO != null) {
					//查询购买人级别低于区代
					int fitNum = splitDao.checkParentLevelAwardFit(memberVO.getLevel(), level);
					if(fitNum > 0){
						// 锁定用户券
						int result = 0;
						do {
							result = splitDao.lockMemberTicket(memberVO.getId());
							if (result == 0) {
								Thread.sleep(100);
							}
						} while (result == 0);
						// 商票
						List<MemberTicketVO> ticketList = null;
						double minRate = splitDao.getMinRate();
						// 1.同折扣销售必须为最低折扣
						if (buy_rate.equals(NumberUtil.formatDouble(minRate, 2))) {
							ticketList = splitDao.getHasTicketMemberArea(memberVO.getId(), buy_rate);
							// 2.分销商 销售折扣< 产生动销奖
						} else {
							ticketList = splitDao.getHasTicketMemberAreaLt(memberVO.getId(), buy_rate);
						}
						if (ticketList.size() != 0) {
							for (MemberTicketVO memberTicketVO : ticketList) {
								// 可卖提货券大于购买提货券
								if (Integer.parseInt(memberTicketVO.getNum()) > leftNum) {
									// 拆分提货券
									// 修改原提货券为剩余提货券
									int re = splitDao.updateSplitTicket(memberTicketVO.getId(), leftNum);
									if (re > 0) {
										// 添加卖掉的提货券并关联订单 关联原提货券单号
										MemberTicketVO ticketVO = new MemberTicketVO();
										splitDao.insertSellTicket(ticketVO, memberTicketVO.getId(), leftNum, order_id,
												memberTicketVO.getMember_id());
										// 奖项分配
										genDynamicAndCityAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, cityAward, city_level, memberVO, leftNum, memberTicketVO,
												ticketVO.getId(),conn_no, country);
										// 提货券销售完毕
										leftNum = 0;
									}
									// 可卖提货券等于购买提货券
								} else if (Integer.parseInt(memberTicketVO.getNum()) == leftNum) {
									// 原提货券关联订单
									int re = splitDao.updateSplitTicketOrderEq(memberTicketVO.getId(), order_id, leftNum);
									if (re > 0) {
										// 奖项分配
										genDynamicAndCityAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, cityAward, city_level, memberVO, leftNum, memberTicketVO,
												memberTicketVO.getId(),conn_no, country);
										// 提货券销售完毕
										leftNum = 0;
									}
									// 可卖提货券小于购买提货券
								} else {
									// 扣除原提货券并关联订单
									int re = splitDao.updateSplitTicketOrderLt(memberTicketVO.getId(), order_id,
											Integer.parseInt(memberTicketVO.getNum()));
									if (re > 0) {
										// 奖项分配
										genDynamicAndCityAward(member_id, type, buy_rate, order_id, dynamicAward,
												staticAward, cityAward, city_level, memberVO, Integer.parseInt(memberTicketVO.getNum()), memberTicketVO,
												memberTicketVO.getId(),conn_no, country);
										// 提货券销售部分
										leftNum = leftNum - Integer.parseInt(memberTicketVO.getNum());
									}
								}
								// 扣除完毕退出该会员提货券循环
								if (leftNum == 0) {
									break;
								}
							}
						}
						// 券锁定解除
						splitDao.unLockMemberTicket(memberVO.getId());
					}
				}
			}
//		}
		
		
		if (leftNum > 0) {
			// 系统售出
			splitDao.insertTiketOutLogSystem(-1, leftNum, buy_rate, pay_type, type, member_id, order_id, conn_no, country);
		}

	}

	private void genDynamicAndCityAward(String member_id, int type, String buy_rate, String order_id,
			AwardVO dynamicAward, AwardVO staticAward, AwardVO cityAward, String city_level, MemberVO memberVO,
			int leftNum, MemberTicketVO memberTicketVO, String ticketId, String conn_no, String country) throws Exception {

		// 生成动销奖日志 售出提货券日志
		genDynamicAwardLog(member_id, type, buy_rate, order_id, dynamicAward, staticAward, leftNum, memberTicketVO,
				ticketId,conn_no, country);

		// 市代奖获得现金收益日志
		splitDao.insertCashInLogCity(type, memberVO.getId(), leftNum + "", cityAward, order_id, member_id, conn_no, country);
		if (type == 1 || type == 3) {
			splitDao.updateCitySettle(type, memberVO.getId(), leftNum + "", cityAward, order_id, member_id, country);
		}
	}

	private void genDynamicAndAreaAward(String member_id, int type, String buy_rate, String order_id,
			AwardVO dynamicAward, AwardVO staticAward, AwardVO areaAward, String area_level, MemberVO memberVO,
			int leftNum, MemberTicketVO memberTicketVO, String ticketId, String conn_no, String country) throws Exception {

		// 生成动销奖日志 售出提货券日志
		genDynamicAwardLog(member_id, type, buy_rate, order_id, dynamicAward, staticAward, leftNum, memberTicketVO,
				ticketId,conn_no, country);

		// 区域奖获得现金收益日志
		splitDao.insertCashInLogArea(type, memberVO.getId(), leftNum + "", areaAward, order_id, member_id, conn_no, country);
		if (type == 1 || type == 3) {
			splitDao.updateAreaSettle(type, memberVO.getId(), leftNum + "", areaAward, order_id, member_id, country);
		}
	}

	private void genDynamicAndParterAward(String member_id, int type, String buy_rate, String order_id,
			AwardVO dynamicAward, AwardVO staticAward, AwardVO partnerAward, String partner_level, String[] ids,
			int leftNum, int i, String parentLevel, MemberTicketVO memberTicketVO, String ticketId, String conn_no, String level, String country) throws Exception {
			// 生成动销奖日志 售出提货券日志
			genDynamicAwardLog(member_id, type, buy_rate, order_id, dynamicAward, staticAward, leftNum, memberTicketVO,
					ticketId,conn_no, country);
		
		if (partner_level.equals(parentLevel) ) {
			// 合伙人获得现金收益日志
			splitDao.insertCashInLogParter(type, ids[i], leftNum + "", partnerAward, order_id, member_id,conn_no, country);
			if (type == 1 || type == 3) {
				splitDao.updatePartnerSettle(type, ids[i], leftNum + "", partnerAward, order_id, member_id, country);
			}
		}
	}

	private void genDynamicAwardLog(String member_id, int type, String buy_rate, String order_id, AwardVO dynamicAward,
			AwardVO staticAward, int leftNum, MemberTicketVO memberTicketVO, String ticket_id, String conn_no, String country)
			throws Exception {
		// 动销日志存ID
		LogTicketVO logTicketVO = new LogTicketVO();
		// 查询是否为特殊点位
		String special_yn = splitDao.getSpecialYn(memberTicketVO.getMember_id());
		if (Double.parseDouble(memberTicketVO.getRate()) < Double.parseDouble(buy_rate)) {
			// 提货券出账日志
			splitDao.insertTiketOutLog(type, memberTicketVO.getMember_id(), leftNum, buy_rate, memberTicketVO.getRate(),
					dynamicAward, order_id, member_id, staticAward, special_yn,memberTicketVO.getState(), conn_no, country);
			// 1.充值2.购物 分润订单生成 动销奖
			// ①现金日志
			splitDao.insertCashInLogDynamic(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
					memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn, logTicketVO, conn_no, country);

			// ②购物专用金日志
			splitDao.insertBuyCashInLogDynamic(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
					memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn, conn_no, country);
			if ("Y".equals(special_yn)) {
				// 增加待结算还款本金
				splitDao.updateMemberSpecialCash(memberTicketVO.getMember_id(), leftNum, memberTicketVO.getRate());
			}

			// 同折扣售出 合伙人
		} else if (memberTicketVO.getRate().equals(buy_rate)) {
			// 提货券出账日志
			splitDao.insertTiketOutLogRateEq(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
					memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn ,memberTicketVO.getState(), conn_no, country);
			// 现金日志
			splitDao.insertCashInLogDynamicRateEq(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
					memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn,conn_no, country);
		}

		// 充值直接结算
		if (type == 1 || type == 3) {
			// 删除扣除提货券
			splitDao.deleteMemberTicket(ticket_id);
			if (Double.parseDouble(memberTicketVO.getRate()) < Double.parseDouble(buy_rate)) {
				// 累加现金
				if ("Y".equals(special_yn)) {
					// 特殊点位 锁定金额中减本金 增加余额 增加购物金
					splitDao.updateSpecialRecharge(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
							memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn,
							logTicketVO, country);
					// 检查特殊点位状态
					splitDao.updateSpecialCancelCheck(memberTicketVO.getMember_id());
				} else {
					// 增加余额 增加购物金
					splitDao.updateRecharge(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
							memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn,
							logTicketVO, country);
				}
				// 同折扣售出 合伙人 
			} else if (memberTicketVO.getRate().equals(buy_rate)) {
				// 增加余额 增加购物金
				splitDao.updateRechargeRateEq(type, memberTicketVO.getMember_id(), leftNum, buy_rate,
						memberTicketVO.getRate(), dynamicAward, order_id, member_id, staticAward, special_yn, country);
			}
		}
	}

	/**
	 * @param order_id
	 *            交易成功 分润结算 ①正常会员 ②特殊会员还本金 ③日志状态更新
	 * @throws Exception
	 */

	public void settleAward(String order_id) throws Exception {
		// 奖项
		AwardVO joinAward = null;
		AwardVO dynamicAward = null;
		AwardVO staticAward = null;
		AwardVO masterAward = null;
		AwardVO partnerAward = null;
		AwardVO areaAward = null;
		AwardVO companyAward = null;
		AwardVO pushAward = null;
		AwardVO cityAward = null;
		// 等级列表
		String common_level = PropertieUtil.loadPropertie("common_level");
		String vip_level = PropertieUtil.loadPropertie("vip_level");
		String primary_level = PropertieUtil.loadPropertie("primary_level");
		String middle_level = PropertieUtil.loadPropertie("middle_level");
		String teacher_level = PropertieUtil.loadPropertie("teacher_level");
		String expert_level = PropertieUtil.loadPropertie("expert_level");
		String master_level = PropertieUtil.loadPropertie("master_level");
		String partner_level = PropertieUtil.loadPropertie("partner_level");
		String area_level = PropertieUtil.loadPropertie("area_level");
		String city_level = PropertieUtil.loadPropertie("city_level");
		// 获取奖项信息
		List<AwardVO> awardList = awardDao.getAwardAll();
		for (AwardVO awardVO : awardList) {
			if ("join_award".equals(awardVO.getCal())) {
				joinAward = awardVO;
			} else if ("dynamic_award".equals(awardVO.getCal())) {
				dynamicAward = awardVO;
			} else if ("static_award".equals(awardVO.getCal())) {
				staticAward = awardVO;
			} else if ("super_award".equals(awardVO.getCal())) {
				masterAward = awardVO;
			} else if ("company_award".equals(awardVO.getCal())) {
				companyAward = awardVO;
			} else if ("push_award".equals(awardVO.getCal())) {
				pushAward = awardVO;
			} else if ("partner_award".equals(awardVO.getCal())) {
				partnerAward = awardVO;
			} else if ("area_award".equals(awardVO.getCal())) {
				areaAward = awardVO;
			} else if ("city_award".equals(awardVO.getCal())) {
				cityAward = awardVO;
			}
		}
		System.out.println("-----------------------------------------------------------------结算准备修改日志-------------------------------------------------------------");
		// 1.所有奖项更新日志状态
		// 1.现金日志入账日志 2.购物金入账日志
		splitDao.updateAwardEarnLogSuccess(order_id);
		splitDao.updateAwardBuyCashLogSuccess(order_id);
		// 提货券售出日志状态更变结算
		splitDao.updateTicketLogSellSuccess(order_id);
		// 2.所有奖项分润结算
		// 入会奖结算
		List<LogEarnVO> earnJoinList = splitDao.getAwardEarnList(order_id, joinAward.getId());
		if (earnJoinList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnJoinList);
		}
		// 升级 购买入会奖的会员如果是普通会员则升级为高级会员
//		splitDao.joinAwardLevelUp(order_id, vip_level, joinAward, common_level);需求更变 入会奖不升级
		// 动销奖 结算
		List<LogEarnVO> dynamicList = splitDao.getAwardEarnList(order_id, dynamicAward.getId());
		for (LogEarnVO logEarnVO : dynamicList) {
			if ("Y".equals(logEarnVO.getSpecial_yn())) {
				// 特殊会员 确认减本金 (还款金额为pay_back_cash-lock_cash,此处减掉lock_cash) 增加余额
				// 增加专用金
				splitDao.updateSpecialCash(logEarnVO);
				// 判断特殊会员状态 如果金额正常则置为正常
				splitDao.updateSpecialCancelCheck(logEarnVO.getMember_id());
			} else {
				splitDao.addAwardCashAndBuyCashOne(logEarnVO);
			}
		}
		// 删除提货券
		splitDao.deleteMemberTicketBuy(order_id);

		// 常销奖
		List<LogEarnVO> earnStaticList = splitDao.getAwardEarnList(order_id, staticAward.getId());
		if (earnStaticList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnStaticList);
		}
		// 大师奖
		List<LogEarnVO> earnMasterList = splitDao.getAwardEarnList(order_id, masterAward.getId());
		if (earnMasterList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnMasterList);
		}
		// 合伙人奖
		List<LogEarnVO> earnCompanyList = splitDao.getAwardEarnList(order_id, companyAward.getId());
		if (earnCompanyList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnCompanyList);
		}
		// 直推奖
		List<LogEarnVO> earnPushList = splitDao.getAwardEarnList(order_id, pushAward.getId());
		if (earnPushList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnPushList);
		}
		// 补贴奖
		List<LogEarnVO> earnPartnerList = splitDao.getAwardEarnList(order_id, partnerAward.getId());
		if (earnPartnerList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnPartnerList);
		}
		// 区代奖
		List<LogEarnVO> earnAreaList = splitDao.getAwardEarnList(order_id, areaAward.getId());
		if (earnAreaList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnAreaList);
		}
		// 市代奖
		List<LogEarnVO> earnCityList = splitDao.getAwardEarnList(order_id, cityAward.getId());
		if (earnCityList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnCityList);
		}
	}

	// 交易退款 price 购买钱数 buy_cash 购物专用金 二选一 pay_type(兑换券,订单表的pay_type)
	// 部分退的时候 退部分钱 销售的全部退和全退一样
	public void payBack(String order_id, String pay_type, String member_id, String price, String buy_cash)
			throws Exception {
		// 1.所有奖项更新日志状态
		// 1.现金日志入账日志 2.购物金入账日志
		splitDao.updateAwardEarnLogFailed(order_id);
		splitDao.updateAwardBuyCashLogFailed(order_id);
		// 查询提货券 增加退卷日志 先加日志 再改状态 满足多次部分退
		List<LogTicketVO> logTicketList = splitDao.getBackTickets(order_id);
		if (logTicketList.size() != 0) {
			for (LogTicketVO logTicketVO : logTicketList) {
				splitDao.insertLogTicketIn5(logTicketVO);
			}
		}
		// 提货券售出日志状态更变取消
		splitDao.updateTicketLogSellFailed(order_id);
		
		// 返还提货券
		splitDao.backMemberTicketBuy(order_id);
		// 1.现金退款入账日志
		// 支付方式为购物金或现金
		if (pay_type != null) {
			if ("4".equals(pay_type)) {
				if (buy_cash != null && !"0".equals(buy_cash)) {
					// 购物金入账日志
					splitDao.insertBuyCashInLog(member_id, order_id, buy_cash);
				}
			} else if (  "3".equals(pay_type)) {
				if (price != null && !"0".equals(price)) {
					// 余额入账日志 (type5购物)
					splitDao.insertCashInLog5(member_id, price, order_id, pay_type);
					//消息+1
					msgDao.updMemberMsg(member_id, "4");
					//推送
				/*	Map map = new HashMap();
					map.put("title", configDao.loadConfig("APP_NAME"));
					map.put("content", "您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入");
					map.put("type", 2);
					//PushUtil.getuiPushSingle(memberDao.getCidById(member_id), configDao.loadConfig("APP_LOGO"), configDao.loadConfig("APP_NAME"),
							"您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入", "",JSON.toJSONString(map));*/
					//pushService.pushPayBack(price,member_id);

				}
			}
		}

		// 入会奖
		AwardVO joinAward = null;
		AwardVO dynamicAward = null;
		AwardVO staticAward = null;
		AwardVO masterAward = null;
		AwardVO partnerAward = null;
		// 等级列表
		String common_level = PropertieUtil.loadPropertie("common_level");
		String vip_level = PropertieUtil.loadPropertie("vip_level");
		String primary_level = PropertieUtil.loadPropertie("primary_level");
		String middle_level = PropertieUtil.loadPropertie("middle_level");
		String teacher_level = PropertieUtil.loadPropertie("teacher_level");
		String expert_level = PropertieUtil.loadPropertie("expert_level");
		String master_level = PropertieUtil.loadPropertie("master_level");
		String partner_level = PropertieUtil.loadPropertie("partner_level");
		// 获取奖项信息
		List<AwardVO> awardList = awardDao.getAwardAll();
		for (AwardVO awardVO : awardList) {
			if ("join_award".equals(awardVO.getCal())) {
				joinAward = awardVO;
			} else if ("dynamic_award".equals(awardVO.getCal())) {
				dynamicAward = awardVO;
			} else if ("static_award".equals(awardVO.getCal())) {
				staticAward = awardVO;
			} else if ("super_award".equals(awardVO.getCal())) {
				masterAward = awardVO;
			} else if ("partner_award".equals(awardVO.getCal())) {
				partnerAward = awardVO;
			}
		}
		//如果是第一次退
		if(logTicketList.size() != 0){
			// 特殊点位还原锁定本金
			List<LogEarnVO> dynamicList = splitDao.getAwardEarnList(order_id, dynamicAward.getId());
			for (LogEarnVO logEarnVO : dynamicList) {
				if ("Y".equals(logEarnVO.getSpecial_yn())) {
					// 特殊会员 减锁定金额 减还款金额
					splitDao.updateSpecialCashFailed(logEarnVO);
					// 判断特殊会员状态 如果金额正常则置为正常
					splitDao.updateSpecialCancelCheck(logEarnVO.getMember_id());
				}
			}
		}
	}

	// 使用提货券购物 扣除提货券 产生日志
	public void useTicketBuy(String member_id, int num, String order_id) {
		if (num == 0) {
			return;
		}
		// 锁定用户券
		int result = 0;
		do {
			result = splitDao.lockMemberTicket(member_id);
			if (result == 0) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		} while (result == 0);
		int leftNum = num;
		// 条件：1.id符合 2.优先使用不可流通 购买时间早 购买rate大的提货券
		List<MemberTicketVO> ticketList = splitDao.getHasTicketMemberBuy(member_id);
		if (ticketList != null) {
			for (MemberTicketVO memberTicketVO : ticketList) {

				// 可卖提货券大于购买提货券
				if (Integer.parseInt(memberTicketVO.getNum()) > leftNum) {
					// 拆分提货券
					int re = splitDao.updateSplitTicket(memberTicketVO.getId(), leftNum);// 修改原提货券为剩余提货券
					if (re > 0) {
						// 添加卖掉的提货券并关联订单 关联原提货券单号
						MemberTicketVO ticketVO = new MemberTicketVO();
						splitDao.insertSellTicket(ticketVO, memberTicketVO.getId(), leftNum, order_id,
								memberTicketVO.getMember_id());

						// 添加提货券消费日志
						splitDao.insertTicketOutLog2(member_id, order_id, leftNum, memberTicketVO.getState(),memberTicketVO.getRate());
						// 提货券销售完毕
						leftNum = 0;
					}

					// 可卖提货券等于购买提货券
				} else if (Integer.parseInt(memberTicketVO.getNum()) == leftNum) {
					// 原提货券关联订单
					int re = splitDao.updateSplitTicketOrderEq(memberTicketVO.getId(), order_id, leftNum);
					if (re > 0) {
						// 添加提货券消费日志
						splitDao.insertTicketOutLog2(member_id, order_id, leftNum, memberTicketVO.getState(),memberTicketVO.getRate());
						// 提货券销售完毕
						leftNum = 0;
					}

					// 可卖提货券小于购买提货券
				} else {
					// 扣除原提货券并关联订单
					int re = splitDao.updateSplitTicketOrderLt(memberTicketVO.getId(), order_id,
							Integer.parseInt(memberTicketVO.getNum()));
					if (re > 0) {
						// 添加提货券消费日志
						splitDao.insertTicketOutLog2(member_id, order_id, Integer.parseInt(memberTicketVO.getNum()),
								memberTicketVO.getState(),memberTicketVO.getRate());
						// 提货券销售部分
						leftNum = leftNum - Integer.parseInt(memberTicketVO.getNum());
					}
				}
				// 扣除完毕退出该会员提货券循环
				if (leftNum == 0) {
					break;
				}
			}

		}
		// 券锁定解除
		splitDao.unLockMemberTicket(member_id);
	}

	// 确认收货并且使用购物券付款
	public void updateTicketLogSellSuccess(String order_id) throws Exception {
		// 入会奖
		AwardVO joinAward = null;
		AwardVO dynamicAward = null;
		AwardVO staticAward = null;
		AwardVO masterAward = null;
		AwardVO partnerAward = null;
		// 等级列表
		String common_level = PropertieUtil.loadPropertie("common_level");
		String vip_level = PropertieUtil.loadPropertie("vip_level");
		String primary_level = PropertieUtil.loadPropertie("primary_level");
		String middle_level = PropertieUtil.loadPropertie("middle_level");
		String teacher_level = PropertieUtil.loadPropertie("teacher_level");
		String expert_level = PropertieUtil.loadPropertie("expert_level");
		String master_level = PropertieUtil.loadPropertie("master_level");
		String partner_level = PropertieUtil.loadPropertie("partner_level");
		// 获取奖项信息
		List<AwardVO> awardList = awardDao.getAwardAll();
		for (AwardVO awardVO : awardList) {
			if ("join_award".equals(awardVO.getCal())) {
				joinAward = awardVO;
			} else if ("dynamic_award".equals(awardVO.getCal())) {
				dynamicAward = awardVO;
			} else if ("static_award".equals(awardVO.getCal())) {
				staticAward = awardVO;
			} else if ("super_award".equals(awardVO.getCal())) {
				masterAward = awardVO;
			} else if ("partner_award".equals(awardVO.getCal())) {
				partnerAward = awardVO;
			}
		}
		splitDao.deleteMemberTicketBuy(order_id);
		// 1.现金日志入账日志  入会奖
		splitDao.updateAwardEarnLogSuccess(order_id);
		// 入会奖结算
		List<LogEarnVO> earnJoinList = splitDao.getAwardEarnList(order_id, joinAward.getId());
		if (earnJoinList.size() != 0) {
			splitDao.addAwardCashAndBuyCash(earnJoinList);
		}
	}

	// 取消订单并且使用购物券付款
	public void payBackUseTicket(String order_id, String pay_type, String member_id, String price, String buy_cash)
			throws Exception {
		// 1.现金日志入账日志 
		splitDao.updateAwardEarnLogFailed(order_id);
		// 查询花的提货券 增加退券日志
		List<LogTicketVO> logTicketUseList = splitDao.getBackTicketsUse(order_id);
		if (logTicketUseList.size() != 0) {
			for (LogTicketVO logTicketVO : logTicketUseList) {
				splitDao.insertLogTicketInUse5(logTicketVO);
			}
		}
		/*// 根据订单锁定的 删除成功的    -----加多了
		List<MemberTicketVO> ticketList = splitDao.getBackLockTicketByOrder(order_id);
		for (MemberTicketVO memberTicketVO : ticketList) {
			splitDao.insertLogTicketInBack5(memberTicketVO, order_id);
		}*/
		// 返还提货券
		splitDao.backMemberTicketBuy(order_id);
		// 1.现金退款入账日志
		// 支付方式为购物金或现金
		if (pay_type != null) {
			if ("4".equals(pay_type)) {
				if (buy_cash != null && !"0".equals(buy_cash)) {
					// 购物金入账日志
					splitDao.insertBuyCashInLog(member_id, order_id, buy_cash);
				}
			} else if (  "3".equals(pay_type)) {
				if (price != null && !"0".equals(price)) {
					// 余额入账日志 (type5购物)
					splitDao.insertCashInLog5(member_id, price, order_id, pay_type);
					//消息+1
					msgDao.updMemberMsg(member_id, "4");
					//推送
					/*Map map = new HashMap();
					map.put("title", configDao.loadConfig("APP_NAME"));
					map.put("content", "您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入");
					map.put("type", 2);
					//PushUtil.getuiPushSingle(memberDao.getCidById(member_id), configDao.loadConfig("APP_LOGO"), configDao.loadConfig("APP_NAME"),
							"您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入", "",JSON.toJSONString(map));*/
					//pushService.pushPayBack(price,member_id);

				}
			}
		}

	}

	// 部分退货 还原退券 num退的券 产生退券的日志
	public void deleteTicketBuyAndGenBackLog(String member_id, int num, String order_id, String pay_type, String price,
			String buy_cash) throws Exception {
		// 1.现金日志入账日志 
		splitDao.updateAwardEarnLogFailed(order_id);
		
		if (num > 0) {
			// 锁定
			int result = 0;
			do {
				result = splitDao.lockMemberTicket(member_id);
				if (result == 0) {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} while (result == 0);
			// 扣券
			int leftNum = num;

			// 根据订单锁定的 删除成功的
			List<MemberTicketVO> ticketList = splitDao.getBackLockTicketByOrder(order_id);
			if (ticketList != null) {
				for (MemberTicketVO memberTicketVO : ticketList) {
					// 可卖提货券大于购买提货券
					if (Integer.parseInt(memberTicketVO.getNum()) > leftNum) {
						// 拆分提货券
						int re = splitDao.updateSplitTicket(memberTicketVO.getId(), leftNum);// 修改原提货券为剩余提货券
						if (re > 0) {
							// 添加退掉的提货券并关联订单
							MemberTicketVO ticketVO = new MemberTicketVO();
							splitDao.insertBackTicket(ticketVO, memberTicketVO.getId(), leftNum, order_id,
									memberTicketVO.getMember_id());
							splitDao.insertLogTicketInBack5Lt(memberTicketVO,leftNum, order_id);
							leftNum = 0;
						}
						// 可卖提货券等于购买提货券
					} else if (Integer.parseInt(memberTicketVO.getNum()) == leftNum) {
						splitDao.backTicketById(memberTicketVO.getId());
						splitDao.insertLogTicketInBack5(memberTicketVO, order_id);
						leftNum = 0;
						// 可卖提货券小于购买提货券
					} else {
						splitDao.backTicketById(memberTicketVO.getId());
						splitDao.insertLogTicketInBack5(memberTicketVO, order_id);
						// 提货券销售部分
						leftNum = leftNum - Integer.parseInt(memberTicketVO.getNum());
					}
					// 扣除完毕退出该会员提货券循环
					if (leftNum == 0) {
						break;
					}
				}
			}

			/*
			 * // 根据订单锁定的 退掉剩下的 List<MemberTicketVO> ticketList =
			 * splitDao.getLockTicketByOrder(order_id); if (ticketList != null)
			 * { for (MemberTicketVO memberTicketVO : ticketList) {
			 * splitDao.insertLogTicketInBack5(memberTicketVO,order_id); } }
			 */
			// 解锁
			splitDao.unLockMemberTicket(member_id);
		}
		// 1.现金退款入账日志
		// 支付方式为购物金或现金
		if ("4".equals(pay_type)) {
			if (buy_cash != null && !"0".equals(buy_cash)) {
				// 购物金入账日志
				splitDao.insertBuyCashInLog(member_id, order_id, buy_cash);
			}
		} else if ( "3".equals(pay_type)) {
			if (price != null && !"0".equals(price)) {
				// 余额入账日志 (type5购物)
				splitDao.insertCashInLog5(member_id, price, order_id, pay_type);
				//消息+1
				msgDao.updMemberMsg(member_id, "4");
				//推送
				/*Map map = new HashMap();
				map.put("title", configDao.loadConfig("APP_NAME"));
				map.put("content", "您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入");
				map.put("type", 2);
				//PushUtil.getuiPushSingle(memberDao.getCidById(member_id), configDao.loadConfig("APP_LOGO"), configDao.loadConfig("APP_NAME"),
						"您有一比" + NumberUtil.formatDouble(Double.parseDouble(price), 2) + "元收入", "",JSON.toJSONString(map));*/
				//pushService.pushPayBack(price,member_id);

			}
		}
	}
	
}
